package com.zc.im.common.config;

import com.zc.im.common.properties.ZkProperties;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Project：im
 * Date：2022/4/13
 * Time：16:10
 * Description：zookeeper自动配置
 *
 * @author Challen.Zhang
 * @version 1.0
 */
@EnableConfigurationProperties(ZkProperties.class)
@Configuration
public class ZkConfiguration {

    private final ZkProperties prop;

    public ZkConfiguration(ZkProperties properties) {
        this.prop = properties;
    }

    @Bean("curatorClient")
    public CuratorFramework curatorClient() {
        CuratorFramework client = CuratorFrameworkFactory.builder()
                //连接地址  集群用,隔开
                .connectString(prop.getIp())
                //连接超时时间
                .connectionTimeoutMs(prop.getConnectionTimeoutMs())
                //会话超时时间
                .sessionTimeoutMs(prop.getSessionTimeOut())
                //设置重试机制
                .retryPolicy(new ExponentialBackoffRetry(prop.getSleepMsBetweenRetry(),prop.getMaxRetries()))
                //设置命名空间 在操作节点的时候，会以这个为父节点
                .namespace(prop.getNamespace())
                .build();
        client.start();

        return client;
    }

}
